<template>
  <div class="reader-container">

    <el-button type="primary" @click="showAddDialog">添加</el-button>

  
    <el-table border :data="tagData" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="60"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="password" label="密码"></el-table-column>
      <el-table-column prop="identity" label="身份">
        <template #default="scope">
          <el-tag v-if="scope.row.identity == 1" type="success">管理员</el-tag>
          <el-tag v-else-if="scope.row.identity == 2" type="warning">作者</el-tag>
          <el-tag v-else type="info">用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="userphone" label="手机号"></el-table-column>
      <el-table-column prop="userstate" label="状态">
        <template #default="scope">
          <el-tag v-if="scope.row.userstate == 0" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog title="编辑用户信息" v-model="editDialogVisible">
      <el-form :model="editForm" label-width="80px" :rules="formRules" ref="editFormRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="身份" prop="identity">
          <el-input v-model="editForm.identity" disabled></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="userphone">
          <el-input v-model="editForm.userphone"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="userstate">
          <el-select v-model="editForm.userstate" placeholder="请选择">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="禁用" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitEdit">确定</el-button>
        </span>
      </template>
    </el-dialog>

 
    <el-dialog title="添加用户信息" v-model="addDialogVisible">
      <el-form :model="addForm" label-width="80px" :rules="formRules" ref="addFormRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="身份" prop="identity">
          <el-input v-model="addForm.identity" disabled></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="userphone">
          <el-input v-model="addForm.userphone"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="userstate">
          <el-select v-model="addForm.userstate" placeholder="请选择">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="禁用" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitAdd">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import axios from 'axios';
import { onMounted } from 'vue';

const tagData = ref([]);
const editDialogVisible = ref(false);
const addDialogVisible = ref(false);
const editForm = ref({
  id: '',
  username: '',
  password: '',
  identity: '用户', // 默认显示为“用户”
  userphone: '',
  userstate: '' // 0:禁用 1:正常
});
const addForm = ref({
  username: '',
  password: '',
  identity: '用户', // 默认显示为“用户”
  userphone: '',
  userstate: '' // 0:禁用 1:正常
});
const formRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  userphone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  userstate: [{ required: true, message: '请选择状态', trigger: 'change' }]
};

onMounted(() => {
  getAllInfos();
});

const getAllInfos = () => {
  axios({
    url: '/api/admin/reader/getAll',
    method: 'POST',
  })
  .then(res => {
    tagData.value = res.data.data;
    ElMessage.success('获取用户信息成功');
  })
  .catch(err => {
    ElMessage.error('获取用户信息失败');
  });
};

const showAddDialog = () => {
  addForm.value = { username: '', password: '', identity: '用户', userphone: '', userstate: '' };
  addDialogVisible.value = true;
};

const submitAdd = () => {
  axios({
    url: '/api/admin/reader/add',
    method: 'POST',
    params: {
      username: addForm.value.username,
      password: addForm.value.password,
      identity: 3, // 返回给后端的值为 3
      userphone: addForm.value.userphone,
      userstate: Number(addForm.value.userstate)
    }
  })
  .then(res => {
    ElMessage.success(res.data.msg);
    getAllInfos();
    addDialogVisible.value = false;
  })
  .catch(err => {
    ElMessage.error(err.message || '请求失败，请稍后再试');
  });
};

const handleEdit = (row) => {
  editForm.value = { ...row, identity: '用户' }; // 编辑时也显示为“用户”
  editDialogVisible.value = true;
};

const submitEdit = () => {
  axios({
    url: '/api/admin/reader/update',
    method: 'POST',
    params: {
      id: editForm.value.id,
      username: editForm.value.username,
      password: editForm.value.password,
      identity: 3, // 返回给后端的值为 3
      userphone: editForm.value.userphone,
      userstate: Number(editForm.value.userstate)
    }
  })
  .then(res => {
    ElMessage.success(res.data.msg);
    getAllInfos();
    editDialogVisible.value = false;
  })
  .catch(err => {
    ElMessage.error(err.message || '请求失败，请稍后再试');
  });
};

const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该用户吗？', '确认删除', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  .then(() => {
    axios({
      url: '/api/admin/reader/delete',
      method: 'POST',
      params: {
        id: row.id
      }
    })
    .then(res => {
      ElMessage.success(res.data.msg);
      getAllInfos();
    })
    .catch(err => {
      ElMessage.error(err.message || '请求失败，请稍后再试');
    });
  })
  .catch(() => {
    ElMessage.info('已取消删除');
  });
};
</script>

<style scoped>
.reader-container {
  margin: 20px;
}
</style>